package com.pay.common.util;

import java.io.Writer;

import org.apache.commons.lang3.StringUtils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.BigDecimalConverter;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;


public class XmlUtils {
    public static boolean cDataFlag;
    /**
     * 创建XStream
     */
    private static XStream createXstream() {
        return new XStream(new XppDriver() {
            @Override
            public HierarchicalStreamWriter createWriter(Writer out) {
                return new PrettyPrintWriter(out) {
                    @Override
                    public void startNode(String name, @SuppressWarnings("rawtypes") Class clazz) {
                        super.startNode(name, clazz);
                    }

                    @Override
                    protected void writeText(QuickWriter writer, String text) {
                        if (cDataFlag) {
                            writer.write(cDATA(text));
                        } else {
                            writer.write(text);
                        }
                    }
                    
                    @Override
                    public String encodeNode(String name) {
                        return name;
                    }

                    private String cDATA(String text) {
                        return "<![CDATA[" + text + "]]>";
                    }

                };
            }
        });
    }

    /**
     *  注解对象转xml
     * @param obj
     * @param cls
     * @param cData true 加cdata标签 false不加
     * @return
     */
    public static String toXML(Object obj, Class<?> cls,boolean cData) {
        if (obj == null) {
            return null;
        }
        cDataFlag = cData;
        XStream xstream = createXstream();
        xstream.processAnnotations(cls);
        return xstream.toXML(obj);
    }

    /**
     * Object 转化 XML
     */
    public static String toXML(Object obj) {
        if (obj == null) {
            return null;
        }
        XStream xstream = new XStream();
        return xstream.toXML(obj);
    }

    /**
     * XML转化为JAVA对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T xml2Obj(String xml, Class<?> cls) {
        if (StringUtils.isBlank(xml)) {
            return null;
        }
        XStream xstream = new XStream();
        if (cls != null) {
            xstream.processAnnotations(cls);
        }
        xstream.registerConverter(new BigDecimalConverter()
        {
            @Override
            public Object fromString(String str) {
                if (StringUtils.isBlank(str)) {
                    return null;
                }
                return super.fromString(str);
            }
        });
        return (T) xstream.fromXML(xml);
    }

    /**
     * XML转化为JAVA对象
     */
    public static <T> T xml2Obj(String xml) {
        return xml2Obj(xml, null);
    }
}
